#ParallelPlatform Project - A web platform to distribute and share knowledge
#Copyright (C) 2011  Daniel Guerrero Martinez & Sergio Rodriguez Lumley
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>.
from ParallelPlatform.wiki.models import Page
from ParallelPlatform.wiki.utils import getRoot

def get_tree():
    pages = Page.objects.filter(valid=True)
    dic="{ "

    for page in pages:
        item = '"'+str(page.uuid)+'":{"id":"'+str(page.uuid)+'","name": "'+page.getRevisionValid().title.strip()+'" ,"children":[ '
        for child in page.children.filter(valid=True):
            item+='"'+str(child.uuid)+'",'
        dic+= item[0:len(item)-1] +"]},"
    
    dic+='"root" : "'+str(getRoot().uuid)+'"}'
    
    return dic
